<template>
  <DataList ref="datalist" :excelHeader="excelHeader" skipExcelNumber="3" :cate="cate" :cols="cols" :name="name" />
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import { addXsjbxx_new, deleteXsjbxx_new, updateXsjbxx_new, getXsjbxx_new, BulkAddXsjbxx_new } from "@/api/补充2020年9月后入学新生信息.js";
import {findComponent} from "@/utils/ui";
import {get开设专业} from '@/api/开设专业.js'
import linq, { from } from "linq"
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: "A11-6 2020年9月后入学新生信息",
      name: '11.6 补充2020年9月后入学新生信息',
      excelHeader:{B3:"基本信息",P3:"行政班信息",B4:"学号",C4:"姓名",D4:"性别",E4:"民族",F4:"学籍状态",G4:"电子邮箱",H4:"生源地",I4:"来自军队",J4:"招生方式",K4:"生源类型",L4:"入学总分",M4:"是否常住户口在农村",N4:"是否订单（定向）培养",O4:"是否建档立卡贫困家庭",P4:"年级",Q4:"学制",R4:"入学日期（年月）",S4:"系部代码",T4:"所属系部",U4:"班级代码",V4:"班级名称（全称）",W4:"专业代码",X4:"专业名称（全称）",Y4:"专业方向代码",Z4:"专业方向名称（全称）"},
      cols: [
        { label: "基本信息", prop: "基本信息" ,data:[
            {label:'学号',prop:'XSXH'},
            { label: "姓名", prop: "XM" },
            {label:'性别',prop:'XZ_XB'},
            {label:'民族',prop:'XZ_MZ'},
            {label:'学籍状态',prop:'XZ_XJZT'},
            { label: "电子邮箱", prop: "DZYX" },
            {label:'生源地',prop:'SYD'},
            {label:'来自军队',prop:'XZ_SFJD'},
            {label:'招生方式',prop:'XZ_ZSFS'},
            { label: "生源类型", prop: "XZ_SYLX" },
            {label:'是否常住户口在农村',prop:'XZ_SFNCHK'},
            {label:'是否订单(定向)培养',prop:'XZ_SFDDPY'},
            {label:'是否建档立卡贫困家庭',prop:'XZ_SFPKH'},
        ]},
        {label:"行政班信息",prop:"",data:[
           { label: "年级", prop: "XZ_NJ" },
            {label:'学制',prop:'XZ_XZ'},
            {label:'入学日期(年月)',prop:'RXRQ'},
            { label: "入学总分", prop: "RXCJZF" },
            {label:'系部代码',prop:'BMBH'},
            {label:'所属系部(全称)',prop:'BMMC'},
            {label:'班级代码',prop:'BJDM'},
            { label: "班级名称(全称)", prop: "BJMC" },
            {label:'专业代码',prop:'ZYDM'},
            {label:'专业名称(全称)',prop:'ZYMC'},
            { label: "专业方向代码", prop: "ZYFXDM" },
            {label:'专业方向名称(全称)',prop:'ZYFXMC'}
        ]},
        {label:'检测列：学号是否重复？',prop:'检测列_学号是否重复'}
      ]
    };
  },
  methods: {
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]="合计"+linq.from(list).distinct("$.XSXH").count();
        sums[4]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
        sums[4] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
        sums[5]= "学生数:"+linq.from(list).distinct("$.XSXH").count();
        sums[7]= "女:"+linq.from(list).where("$.XZ_XB=='女'").count();
        sums[8]= "少数民族:"+(linq.from(list).where("$.XZ_MZ").count()-linq.from(list).where("$.XZ_MZ=='汉族'").count());
        sums[9]= "辍学数:"+linq.from(list).where("$.XZ_XJZT=='辍学'").count();
        sums[12]= "军队:"+(linq.from(list).where("$.XZ_SFJD").count()-linq.from(list).where("$.XZ_SFJD=='否'").count());
        sums[15]= "农村:"+linq.from(list).where("$.XZ_SFNCHK=='是'").count();
        sums[16]= "订单:"+linq.from(list).where("$.XZ_SFDDPY=='是'").count();
        sums[17]= "贫困:"+linq.from(list).where("$.XZ_SFPKH=='是'").count();
        sums[23]= "系部数:"+linq.from(list).distinct("$.BMMC").count();
        sums[25]= "班级数:"+linq.from(list).distinct("$.BJMC").count();
        sums[26]= "专业数:"+linq.from(list).distinct("$.ZYMC").count();
        sums[28]= "方向数:"+linq.from(list).distinct("$.ZYFXMC").count();
        
       return sums;
      //
    },
    check(array,list) {
      return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          // var 专业 = await get开设专业(user.XXID,user.CJPC)
          // var err = linq.from(array).select("$.BMMC+','+$.ZYDM+','+$.ZYFXDM").except(linq.from(专业).select("$.BMMC+','+$.ZYDM+','+$.ZYFXDM").toArray()).toArray();
          // linq.from(array).where(e=>err.indexOf(e.BMMC+','+e.ZYDM+','+e.ZYFXDM) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"不存在系部名称,专业和专方向代码组合");
        
          var err =  linq.from(array).select("$.XSXH").intersect(linq.from(list).select("$.XSXH").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.XSXH) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"已经存在导入的学号");
          
           resolve();
          }catch(e){
            reject(e);
          }
     })  
    },
    onDlgCreated(model) {
      var dd = findComponent(this,"BMMC");
      var 专业 = linq.from(this.$root.开设专业).distinct("$.BMMC").toArray();
      dd.$attrs.items = 专业.map(item => {
        return { label: item.BMMC, value: item.BMMC};
      });
      
    },
    //别的页面不用复制此方法
    
    onChange(model, field, value) {
      //
      
      if (field == "BMMC") {
        var zydm =  linq.from(this.$root.开设专业).where(item=>item.BMMC==value).distinct("$.ZYDM").toArray();
        var zyfxdm = linq.from(this.$root.开设专业).where(item=>item.BMMC==value).distinct("$.ZYFXDM").toArray();
        var dd = findComponent(this,"ZYMC");
        dd.$attrs.items =zydm.map(item => {
          return { value: item.ZYMC, label: item.ZYMC,zydm:item.ZYDM };
        });
        var fx = findComponent(this,"ZYFXMC");
        fx.$attrs.items =zyfxdm.map(item => {
          return { value: item.ZYFXMC, label: item.ZYFXMC,zyfxdm:item.ZYFXDM };
        });
        model.ZYMC = dd.$attrs.items[0].value;
        model.ZYDM = dd.$attrs.items[0].zydm;
        model.ZYFXMC = fx.$attrs.items[0].value;
        model.ZYFXDM = fx.$attrs.items[0].zyfxdm;
        //dd.$children[0].setCurrentValue(dd.$attrs.items[0].ZYDM);
      }
      if(field == "ZYMC"){
        
        var zydm =  linq.from(this.$root.开设专业).where(item=>item.ZYMC==value).toArray();
        //var dd = findComponent(this,"ZYDM");
        model.ZYDM = zydm[0].ZYDM;
      }
      if(field == "ZYFXMC"){
        var zyfxdm =  linq.from(this.$root.开设专业).where(item=>item.ZYFXMC==value).toArray();
        model.ZYFXDM = zyfxdm[0].ZYFXDM;
      }
      //
      model.ZYFXMC = model.ZYMC;
    },
     add_check(model,list){
      //
          //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.XSXH ==  model.XSXH);
          
          if(first)
          return "已经存在对应学号"
      //
    },
    onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
      try{
       
          console.log(JSON.stringify(temp));
          var model = await addXsjbxx_new(temp);
          resolve(model);
          }catch(e){
        reject(e);
      }
      })
      
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateXsjbxx_new(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await deleteXsjbxx_new(temp);
          resolve();
      })
    },
    async fetchData() {
      var user = getUser();
      var datas = await getXsjbxx_new(user.XXID,user.CJPC);
      return datas;
    },
    async  bulkAdd(array){
       await  BulkAddXsjbxx_new({Xsjbxx_new:array});
    }
  }
};
</script>
<style>
</style>